
import Vue from 'vue'
Vue.use({
    install(Vue){
        Vue.prototype.$fromDate = function (time){
                  //计算旧时间和现在时间相减 得到时间中间的 剩下的时间；
          //当前时间
          let timeNow = Date.now()
          let residue = time - timeNow
          residue = residue <= 0 ? residue * -1 : residue
          let getyear = parseInt(residue / 60 / 60 / 24 / 30 / 12 / 1000) % 365
          let month = parseInt(residue / 60 / 60 / 24 / 30 / 1000 % 12);       //月
          let getDate = parseInt(residue / 60 / 60 / 24 / 1000 % 30);    //天
          let getHours = parseInt(residue / 60 / 60 / 1000 % 24);   //小时
          let minute = parseInt(residue / 60 / 1000 % 60);    //分钟
          let second = parseInt(residue / 1000 % 60);   //秒
          let millisecond = (parseInt(residue / 10) + '')
          millisecond = (millisecond).substring(millisecond.length - 2)
          if (getyear < 1 && month >= 1) {
              return month + '月前'
          } else if (getyear < 1 && month < 1 && month < 1 && getDate >= 1) {
              return getDate + '天前'
          } else if (getyear < 1 && month < 1 && getDate < 1 && getHours >= 1) {
              return getHours + '小时前'
          } else if (getyear < 1 && month < 1 && getDate < 1 && getHours < 1 && minute >= 1) {
              return minute + '分钟前'
          } else if (getDate < 1 && month < 1 && getHours < 1 && minute < 1 &&second>=1||millisecond>=1) {
              return '刚刚'
          } else {
              return getyear + '年前'
          }
        }
        Vue.prototype.$WriteTime = function (residue){
          residue = residue <= 0 ? residue * -1 : residue
          let getyear = parseInt(residue / 60 / 60 / 24 / 30 / 12 / 1000) % 365
          let month = parseInt(residue / 60 / 60 / 24 / 30 / 1000 % 12);       //月
          let getDate = parseInt(residue / 60 / 60 / 24 / 1000 % 30);    //天
          let getHours = parseInt(residue / 60 / 60 / 1000 % 24);   //小时
          let minute = parseInt(residue / 60 / 1000 % 60);    //分钟
          let second = parseInt(residue / 1000 % 60);   //秒
          let millisecond = (parseInt(residue / 10) + '')
          millisecond = (millisecond).substring(millisecond.length - 2)
          if (getyear < 1 && month >= 1) {
            return month + '月'
        } else if (getyear < 1 && month < 1 && month < 1 && getDate >= 1) {
            return getDate + '天'
        } else if (getyear < 1 && month < 1 && getDate < 1 && getHours >= 1) {
            return getHours + '小时'
        } else if (getyear < 1 && month < 1 && getDate < 1 && getHours < 1 && minute >= 1) {
            return minute + '分钟'
        } else if (getDate < 1 && month < 1 && getHours < 1 && minute < 1&&second>=1||millisecond>=1) {
            return  second+'秒'
        } else {
            return getyear + '年'
        }
        }
        Vue.directive("throttle", {
          inserted(el, binding, vnode) {
            let delay = Object.keys(binding.modifiers)
           el.scroll =   throttle(binding.value,delay[0])
          },
        });
    }
})